﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;

namespace YiYiTong.Rocket.Api.Biz
{
    public class ShoppingCartService : ApiService
    {
        /// <summary>
        /// 加入购物车
        /// </summary>
        public void Add(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long goods_id = dic.GetValue<long>("goods_id", 0);
            if (goods_id <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = ShoppingCartBiz.Instance.Add(apiContext.CurrUserId, goods_id);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 修改购物车商品数量
        /// </summary>
        public void Edit(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long goods_id = dic.GetValue<long>("goods_id", 0);
            int num = dic.GetValue<int>("num", 0);
            if (goods_id <= 0 || num <= 0 || num > 99)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "参数错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = ShoppingCartBiz.Instance.Edit(apiContext.CurrUserId, goods_id, num);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 删除购物车商品
        /// </summary>
        public void Del(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long seq_id = dic.GetValue<long>("seq_id", 0);
            if (seq_id <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = ShoppingCartBiz.Instance.Del(seq_id);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 删除所选择的商品
        /// </summary>
        public void DelSelect(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string seq_ids = dic.GetValue<string>("seq_ids");
            if (string.IsNullOrWhiteSpace(seq_ids))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = ShoppingCartBiz.Instance.DelSelect(apiContext.CurrUserId, seq_ids);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取购物车商品
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetAll(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            var result = ShoppingCartBiz.Instance.GetAll(apiContext.CurrUserId);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { list = result.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "购物车为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }
    }
}